package orb.transporte;


public class Buffer {

	private byte[] data;
	private static Buffer buf;
	
	public Buffer(){
	}
	
	public static Buffer getInstance(){
		if(buf == null){
			buf = new Buffer();
		}
		return buf;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public void setData(String data) {
		this.data = data.getBytes();
	}

	public void addStringData(String data) {
		this.data = this.concat(this.data, data.getBytes());
	}

	public void addData(byte[] data) {
		if (this.data == null) {
			this.data = data;
		} else {
			this.data = this.concat(this.data, data);
		}
	}

	private byte[] concat(byte[] a, byte[] b) {
		int total = a.length + b.length;
		byte[] resposta = new byte[total];

		int i = 0;
		for (i = 0; i < a.length; i++) {
			resposta[i] = a[i];
		}
		int k = 0;
		for (; k < b.length; i++, k++) {
			resposta[i] = b[k];
		}

		return resposta;
	}
	
	public void cleanBuffer(){
		this.data = null;
		//Buffer.buf = null;
	}

}
